package ui.activity.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import net.ck.hzq.R;

import java.util.List;

import javax.inject.Inject;

import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.HeaderAndFooterRecyclerViewAdapter;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.wallet.biz.RewardsBiz;
import ui.activity.wallet.component.DaggerRewardsComponent;
import ui.activity.wallet.contract.RewardsContract;
import ui.activity.wallet.module.RewardsModule;
import ui.activity.wallet.presenter.RewardsPresenter;
import ui.adapter.RewardsAdapter;
import ui.model.RewardsBean;


/**
 * 新钱包奖励列表
 * Created by chenjian on 2017/8/4.
 */

public class RewardsAct extends BaseAct implements RewardsContract.View {


    @Inject
    RewardsPresenter presenter;
    @Inject
    RewardsBiz biz;

    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    RewardsAdapter adp;


    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_rewards);
    }

    @Override
    protected void setup() {
        DaggerRewardsComponent.builder().rewardsModule(new RewardsModule(this)).build().inject(this);
        setToolBar("详情", "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                presenter.getListInfo(false);
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getListInfo(true);
            }
        });
        rv.setOnItemClickListener(new HeaderAndFooterRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, int position) {
                startAct(RewardsSummaryAct.class, adp.getData().get(position));
            }
        });
        ptr.autoRefresh(true);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Override
    public void upDateUI(List<RewardsBean> bean) {
        if (adp == null) {
            adp = new RewardsAdapter(RewardsAct.this, bean);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }

}
